//封装ajax，传入大对象
function ajax(options) {
  //默认参数
  let default_option = {
    url: "",
    methods: "GET",
    async: true,
    data: {},
    headers: {},
    success: function () {
      console.log("success");
    },
    fail: function () {
      console.log("fail");
    },
  };
  //获取参数变量
  let { url, methods, async, data, headers, success, fail } = {
    ...default_option,
    ...options,
  };

  let xhr = new XMLHttpRequest();
  //传入对象
  let mes = "";
  //200-299的正则表达式
  let pat = /2\d\d/;
  xhr.open(methods, url, async);

  //判断头文件
  if (headers["Content-Type"].includes("json")) {
    xhr.setRequestHeader("Content-Type", headers["Content-Type"]);
    mes = JSON.stringify(data);
  }

  xhr.onload = function () {
    if (xhr.status) {
      success();
    } else {
      fail();
    }
  };

  xhr.send(mes);
}
